home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Devices / CD-ROM / iso9660 / Srcs / support.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-28  |  9.2 KB  |  413 lines  |  [TEXT/MPS ]

  1. /************************************************************************
  2.  *
  3.  *    Copyright © 1990    Apple Computer, Inc.  All rights reserved.
  4.  *
  5.  ************************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10.  
  11. #include <Types.h>
  12. #include <QuickDraw.h>
  13. #include <Packages.h>
  14. #include <StandardFile.h>
  15.  
  16. #include "support.h"
  17.  
  18. #include "support.proto.h"
  19.  
  20. typedef    unsigned char    byte;
  21.  
  22.  
  23. /************************************************************************
  24.  *
  25.  *  Function:        pStrCopy
  26.  *
  27.  *  Purpose:        copy a pascal string from p1 to p2
  28.  *
  29.  *  Returns:        nothing
  30.  *
  31.  *  Side Effects:    p2 gets filled with contents of p1
  32.  *
  33.  *  Description:    simple loop, copying from p1 to p2 for length
  34.  *                    determined by first byte of p1.
  35.  *
  36.  ************************************************************************/
  37. void
  38. pStrCopy(StringPtr p1, StringPtr p2)
  39. {
  40.     register short len;
  41.     
  42.     len = *p2++ = *p1++;
  43.     while (--len>=0)
  44.         *p2++=*p1++;
  45. }
  46.  
  47.  
  48. /************************************************************************
  49.  *
  50.  *  Function:        pStrLen
  51.  *
  52.  *  Purpose:        return length of pascal string
  53.  *
  54.  *  Returns:        short
  55.  *
  56.  *  Side Effects:    none
  57.  *
  58.  *  Description:    The first byte of a pascal string contains the
  59.  *                    length of the string.  Return that value.
  60.  *
  61.  ************************************************************************/
  62. short
  63. pStrLen(StringPtr p)
  64. {
  65.     return (p[0]);
  66. }
  67.  
  68.  
  69. /************************************************************************
  70.  *
  71.  *  Function:        CreateISOName
  72.  *
  73.  *  Purpose:        Create ISO name from pstring.
  74.  *
  75.  *  Returns:        nothing
  76.  *
  77.  *  Side Effects:    dest is filled.
  78.  *
  79.  *  Description:    An ISO file name is 31 characters plus '.;1', all
  80.  *                    in uppercase.  Copy and convert file name, with
  81.  *                    any bogus characters converted to underscore '_'.
  82.  *                    If there was no period in the file name,
  83.  *                    add one.  Add ";1" to the end of the file name.
  84.  *
  85.  ************************************************************************/
  86. short
  87. CreateISOName(char *dest, StringPtr src)
  88. {
  89.     short    i;
  90.     short    limit;
  91.     short    nameSize;
  92.     Boolean    madeDot;
  93.         
  94.     limit = pStrLen(src);
  95.     
  96.     dest[0] = toupper(src[1]);
  97.     
  98.     if (dest[0] == '\000' || dest[0] == '\001')
  99.         return 1;    /* don't add version stuff to myself or parent */
  100.  
  101.     for (i = 1; i < limit; i++)
  102.         dest[i] = (isalnum(src[i+1])) ? toupper(src[i+1]) : '_';
  103.     
  104.  
  105.     madeDot = false;
  106.     for (i = limit; i > 0 && madeDot == false; i--)
  107.         if (dest[i] == '_')
  108.         {
  109.             dest[i] = '.';
  110.             madeDot = true;
  111.         }
  112.  
  113.     nameSize = limit;
  114.     
  115.     if (madeDot == false)        /* we don't have a period. Add one */
  116.         dest[nameSize++] = '.';
  117.     dest[nameSize++] = ';';
  118.     dest[nameSize++] = '1';        /* version number */
  119.  
  120.     return nameSize;
  121. }
  122.  
  123. /************************************************************************
  124.  *
  125.  *  Function:        HFSFile
  126.  *
  127.  *  Purpose:        get the name and vRefNum of an HFS File
  128.  *
  129.  *  Returns:        Boolean
  130.  *                        true    a file was selected
  131.  *                        false    no file. Please stop
  132.  *
  133.  *  Side Effects:    *fn and *vRefNum get filled in
  134.  *
  135.  *  Description:    Call SFGetFile to get the file to be operated upon.
  136.  *
  137.  ************************************************************************/
  138. Boolean
  139. HFSFile(StringPtr fn, short *vRefNum)
  140. {
  141.     Point    SFGwhere;
  142.     SFReply    reply;
  143.     
  144.     SFGwhere.v = 90;
  145.     SFGwhere.h = 82;
  146.     SFGetFile(SFGwhere, (StringPtr)"\pChoose File to Add to ISO disk", 0L, -1, 0L, 0L, &reply );
  147.     if (reply.good) {
  148.         pStrCopy( reply.fName, fn );
  149.         *vRefNum = reply.vRefNum;
  150.         return(true);
  151.     }
  152.     else return(false);
  153. }
  154.  
  155.  
  156.  
  157. /************************************************************************
  158.  *
  159.  *  Function:        ClearOut
  160.  *
  161.  *  Purpose:        set memory to zeros
  162.  *
  163.  *  Returns:        nothing
  164.  *
  165.  *  Side Effects:    *buffer is zeroed out
  166.  *
  167.  *  Description:    zero out buffer for count bytes.
  168.  *
  169.  ************************************************************************/
  170. void
  171. ClearOut(Ptr buffer, short count)
  172. {
  173.     short    i;
  174.     
  175.     for (i = 0; i < count; i++)
  176.         buffer[i] = 0;
  177. }
  178.  
  179.  
  180. /************************************************************************
  181.  *
  182.  *  Function:        SpaceOut
  183.  *
  184.  *  Purpose:        set memory to spaces
  185.  *
  186.  *  Returns:        nothing
  187.  *
  188.  *  Side Effects:    *buffer is spaced out
  189.  *
  190.  *  Description:    Put spaces into buffer for count bytes.
  191.  *
  192.  ************************************************************************/
  193. void
  194. SpaceOut(Ptr buffer, short count)
  195. {
  196.     short    i;
  197.     
  198.     for (i = 0; i < count; i++)
  199.         buffer[i] = ' ';
  200. }
  201.  
  202.  
  203. /************************************************************************
  204.  *
  205.  *  Function:        CharCopy
  206.  *
  207.  *  Purpose:        copy and fill with blanks
  208.  *
  209.  *  Returns:        nothing
  210.  *
  211.  *  Side Effects:    *dest is filled with contents of src & blanks
  212.  *
  213.  *  Description:    copy from *src to *dest for the length specified.
  214.  *                    src is assumed to point to a C null-delimited string.
  215.  *                    If src is smaller than length in size, dest is filled
  216.  *                    with blanks.
  217.  *
  218.  ************************************************************************/
  219. void
  220. CharCopy(char *dest, char *src, short length)
  221. {
  222.     short     i;
  223.     short    j;
  224.     
  225.     i = 0;
  226.     while (*dest++ = *src++)
  227.         i++;
  228.     
  229.     *dest--;
  230.     i--;    /* so that no null terminator is left over */
  231.     
  232.     if (i < length)
  233.         for (j = i; j < length; j++)
  234.             *dest++ = ' ';
  235. }
  236.  
  237.  
  238.  
  239.  
  240. /************************************************************************
  241.  *
  242.  *  Function:    NormalizeLong
  243.  *
  244.  *  Purpose:    normalize a long number that's in lsb format
  245.  *
  246.  *  Returns:    long
  247.  *
  248.  *  Side Effects:    none
  249.  *
  250.  *  Description:
  251.  *                takes a long in lsb format order and converts it
  252.  *                to msb format order.  For example, the long value
  253.  *                0x12345678 becomes 0x78563412
  254.  *                0x78563412 becomes 0x12345678
  255.  *
  256.  *
  257.  ************************************************************************/
  258. long
  259. NormalizeLong(long incoming)
  260. {
  261.     byte    *byteArray;
  262.     long    result;
  263.     
  264.     byteArray = (byte *) &incoming;
  265.     result = (long) byteArray[0] | 
  266.              (long) byteArray[1] << 8 | 
  267.              (long) byteArray[2] << 16 |
  268.              (long) byteArray[3] << 24;
  269.     
  270.     return result;
  271. }
  272.  
  273. /************************************************************************
  274.  *
  275.  *  Function:    NormalizeWord
  276.  *
  277.  *  Purpose:    normalize a word number that's in lsb format
  278.  *
  279.  *  Returns:    word
  280.  *
  281.  *  Side Effects:    none
  282.  *
  283.  *  Description:
  284.  *                takes a word in lsb format order and converts it
  285.  *                to msb format order.  For example, the word value
  286.  *                0x1234 becomes 0x3412
  287.  *                0x3412 becomes 0x1234
  288.  *
  289.  *
  290.  ************************************************************************/
  291. short
  292. NormalizeWord(short incoming)
  293. {
  294.     byte    *byteArray;
  295.     short    result;
  296.     
  297.     byteArray = (byte *) &incoming;
  298.     result = (short) byteArray[0] | 
  299.              (short) byteArray[1] << 8;
  300.     
  301.     return result;
  302. }
  303.  
  304.  
  305. /************************************************************************
  306.  *
  307.  *  Function:    NormalizeVolumeName
  308.  *
  309.  *  Purpose:    convert a string to conform to ISO 9660 naming standards
  310.  *
  311.  *  Returns:    none
  312.  *
  313.  *  Side Effects:    changes the string "someString"
  314.  *
  315.  *  Description:
  316.  *                ISO 9660 forces volume names to be only alphanumeric
  317.  *                characters plus underscore.  This routine converts a
  318.  *                string of arbitrary length to such a volume name string,
  319.  *                converting all illegal characters to underscore.
  320.  *
  321.  *
  322.  ************************************************************************/
  323. void
  324. NormalizeVolumeName(char *someString)
  325. {
  326.     short    i;
  327.     
  328.     for (i = 0; i < strlen(someString); i++)
  329.         someString[i] = (isalnum(someString[i])) ? toupper(someString[i]) : '_';
  330. }
  331.  
  332.  
  333. /************************************************************************
  334.  *
  335.  *  Function:        GetFileInfo
  336.  *
  337.  *  Purpose:        get lengths of file rsrc and data forks
  338.  *
  339.  *  Returns:        OSErr
  340.  *                        noErr, unless PBHGetVInfo has an error.
  341.  *
  342.  *  Side Effects:    dataLength & rsrcLength are changed
  343.  *
  344.  *  Description:     call PBGetFInfo() and return its results.
  345.  *
  346.  ************************************************************************/
  347. OSErr
  348. GetFileInfo(StringPtr name, short vRefNum, long *rsrcLength, long *dataLength, OSType *fType, OSType *fCreator, short *flags)
  349. {
  350.     HParamBlockRec    io;
  351.     OSErr            result;
  352.     
  353.     io.fileParam.ioCompletion = 0L;
  354.     io.fileParam.ioNamePtr = name;
  355.     io.fileParam.ioVRefNum = vRefNum;
  356.     io.fileParam.ioFVersNum = 0;
  357.     io.fileParam.ioFDirIndex = 0;
  358.     result = PBGetFInfo((ParmBlkPtr)&io, false);
  359.     if (result == noErr)
  360.     {
  361.         *rsrcLength = io.fileParam.ioFlRLgLen;
  362.         *dataLength = io.fileParam.ioFlLgLen;
  363.         *fType = io.fileParam.ioFlFndrInfo.fdType;
  364.         *fCreator = io.fileParam.ioFlFndrInfo.fdCreator;
  365.         *flags = io.fileParam.ioFlFndrInfo.fdFlags;
  366.     }
  367.     else
  368.     {
  369.         *rsrcLength = 0L;
  370.         *dataLength = 0L;
  371.         *fType = 0L;
  372.         *fCreator = 0L;
  373.         *flags = 0;
  374.     }
  375.     return result;
  376. }
  377.  
  378.  
  379. /************************************************************************
  380.  *
  381.  *  Function:        GetFinderFlags
  382.  *
  383.  *  Purpose:        get finder flags for a file
  384.  *
  385.  *  Returns:        OSErr
  386.  *                        noErr, unless PBGetFInfo has an error.
  387.  *
  388.  *  Side Effects:    flags value is changed
  389.  *
  390.  *  Description:     call PBGetFInfo() and return its results.
  391.  *
  392.  ************************************************************************/
  393. OSErr
  394. GetFinderFlags(StringPtr name, short vRefNum, short *flags)
  395. {
  396.     HParamBlockRec    io;
  397.     OSErr            result;
  398.     
  399.     io.fileParam.ioCompletion = 0L;
  400.     io.fileParam.ioNamePtr = name;
  401.     io.fileParam.ioVRefNum = vRefNum;
  402.     io.fileParam.ioFVersNum = 0;
  403.     io.fileParam.ioFDirIndex = 0;
  404.     result = PBGetFInfo((ParmBlkPtr)&io, false);
  405.     if (result == noErr)
  406.         *flags = io.fileParam.ioFlFndrInfo.fdFlags;
  407.     else
  408.         *flags = 0;
  409.     return result;
  410. }
  411.  
  412.  
  413.